/**
 * ReportGear(2011)
 */
package com.reportgear.report.core.background;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Shape;
import java.util.Map;
import java.util.WeakHashMap;

import com.reportgear.core.api.Cloneable;
import com.reportgear.report.view.grid.util.GraphHelper;

/**
 * 颜色背景
 * 
 * @version 1.0 2011-3-18
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since Report 1.0
 */
public class ColorBackground extends AbstractBackground implements Cloneable<ColorBackground> {

	private static Map<Color, ColorBackground> initializeCBG = new WeakHashMap<Color, ColorBackground>();
	private Color color = null;

	public static ColorBackground getInstance() {
		return getInstance(null);
	}

	public static ColorBackground getInstance(Color color) {
		ColorBackground ground = initializeCBG.get(color);
		if (ground != null) {
			return ground;
		}

		ColorBackground background = new ColorBackground(color);

		initializeCBG.put(color, background);

		return background;
	}

	private ColorBackground() {
	}

	private ColorBackground(Color color) {
		this.color = color;
	}

	public Color getColor() {
		return this.color;
	}

	@Override
	public void paint(Graphics g, Shape shape) {
		Color color = getColor();

		if (color == null) {
			return;
		}

		Graphics2D g2 = (Graphics2D) g;
		Paint oldPaint = g2.getPaint();

		g2.setPaint(color);

		GraphHelper.fill(g2, shape);

		g2.setPaint(oldPaint);
	}

	@Override
	public ColorBackground clone() {
		return this;
	}
}